/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yy.bugzilla.desk;

import java.util.HashMap;
import javafx.collections.ObservableList;
import javafx.geometry.Side;
import javafx.scene.Node;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;

/**
 *
 * @author hualun-alan
 */
public class MainTabPane extends TabPane {

    HashMap<String, BugListPane> bugListPaneMap = new HashMap<>();

    public MainTabPane() {
        setSide(Side.BOTTOM);
    }

    public BugListPane getBugListPane(String title) {
        return bugListPaneMap.get(title);
    }

    public HashMap<String, BugListPane> getBugListPaneMap() {
        return bugListPaneMap;
    }

    public void addBugListPane(String title, BugListPane listPane) {
        bugListPaneMap.put(title, listPane);
        addTab(title, listPane.getContextPane());
    }

    public void addTab(String title, Node node) {
        addTab(title, node, true);
    }

    public void addTab(String title, Node node, boolean replace) {
        ObservableList<Tab> tabs = getTabs();

        int existIndex = -1;
        for (int i = 0; i < tabs.size(); i++) {
            Tab tab = tabs.get(i);
            if (tab.getText().equals(title)) {
                existIndex = i;
                break;
            }
        }

        if (existIndex > -1) {
            if (replace) {
                Tab tab = new Tab(title);
                tab.setContent(node);
                tabs.set(existIndex, tab);
            }
            getSelectionModel().select(existIndex);
        } else {
            Tab tab = new Tab(title);
            tab.setContent(node);
            tabs.add(tab);
            getSelectionModel().select(tab);
        }
    }

    public int getIndexByTitle(String title) {
        ObservableList<Tab> tabs = getTabs();

        for (int i = 0; i < tabs.size(); i++) {
            Tab tab = tabs.get(i);
            if (tab.getText().equals(title)) {
                return i;
            }
        }

        return -1;
    }

    public void selectTab(String title) {
        int index = getIndexByTitle(title);
        if (index > -1) {
            getSelectionModel().select(index);
        }
    }

    public boolean containsTab(String title) {
        return getIndexByTitle(title) > -1;
    }
}
